package sanshui.system.trade.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import sanshui.system.trade.common.CommonResponse;
import sanshui.system.trade.common.GlobalContain;
import sanshui.system.trade.db.entity.TradeReport;
import sanshui.system.trade.factory.trade.TradeSystemExecutor;
import sanshui.system.trade.factory.trade.indicator.IndicatorCode;
import sanshui.system.trade.factory.trade.strategy.StrategyCode;
import sanshui.system.trade.model.TradeWorkerModel;
import sanshui.system.trade.pojo.ao.CreateBackTradeAo;
import sanshui.system.trade.pojo.vo.TradeReportVo;
import sanshui.system.trade.service.ITradeReportService;
import sanshui.system.trade.service.IWorkerService;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@Slf4j
public class TradeController {
    @Resource
    IWorkerService workerService;
    @Resource
    ITradeReportService tradeReportService;

    /**
     * 回测：创建acc、worker
     *
     * */
    @ApiOperation(value = "创建回测, 可以没有账户")
    @RequestMapping(value = "/createBackTrade", method = RequestMethod.POST)
    public CommonResponse<String> createBackTrade(CreateBackTradeAo backTradeAo) {
        TradeWorkerModel tradeWorkerModel = new TradeWorkerModel();
        BeanUtil.copyProperties(backTradeAo, tradeWorkerModel);

        tradeWorkerModel.setStrategyParam(backTradeAo.getExtraTradeParams());

        log.info("创建回测, info:{}", JSONObject.toJSONString(tradeWorkerModel));
        String workerId = workerService.insertBackTradeWorkerIfDefault(tradeWorkerModel);
        return CommonResponse.ok(workerId,"创建成功");
    }

    @ApiOperation(value = "获取Acc报告")
    @GetMapping("/report/getAcc/{accId}")
    public CommonResponse<List<TradeReportVo>> getAcc(@PathVariable("accId") Long accId) {
        List<TradeReport> reports = tradeReportService.findReportsByAccIdOrWorkerId(accId, null);
//        if (reports.size() > 300){
//            reports = reports.subList(reports.size()-300, reports.size());
//        }
        List<TradeReportVo> res = new ArrayList<>();
        for (TradeReport report : reports) {
            TradeReportVo reportVo = new TradeReportVo();
            BeanUtil.copyProperties(report, reportVo);
            reportVo.setTotalValue(report.getTotalValue());
            res.add(reportVo);
        }
        return CommonResponse.ok(res);
    }

    @ApiOperation(value = "获取worker报告")
    @GetMapping("/report/getWorker/{workerId}")
    public CommonResponse<List<TradeReportVo>> getWorker(@PathVariable("workerId") Long workerId) {
        List<TradeReport> reports = tradeReportService.findReportsByAccIdOrWorkerId(null, workerId);
        List<TradeReportVo> res = new ArrayList<>();
        for (TradeReport report : reports) {
            TradeReportVo reportVo = new TradeReportVo();
            BeanUtil.copyProperties(report, reportVo);
            res.add(reportVo);
        }
        return CommonResponse.ok(res);
    }

    @ApiOperation(value = "获取策略列表")
    @RequestMapping(value = "/query/strategyList", method = RequestMethod.GET)
    public CommonResponse<List<String>> strategyList() {
        List<String> res = new ArrayList<>();
        for (StrategyCode value : StrategyCode.values()) {
            res.add(value.getCode());
        }
        return CommonResponse.ok(res,"查询成功");
    }

    @ApiOperation(value = "获取指标列表")
    @RequestMapping(value = "/query/indicatorList", method = RequestMethod.GET)
    public CommonResponse<List<IndicatorCode>> indicatorList() {
        List<IndicatorCode> res = new ArrayList<>();
        Collections.addAll(res, IndicatorCode.values());
        return CommonResponse.ok(res,"查询成功");
    }
}
